<?php
class Admin_model extends CI_Model {
	
	function __construct()
    {
        parent::__construct();
    }
	
	function get_list($table,$pagesize='100',$offset='0',$where=NULL,$order_by='sort DESC,id DESC'){
	    if (! empty($where)){
	       $this->db->where($where);
	    }
	    $this->db->order_by($order_by);
	    $this->db->limit($pagesize,$offset);
	    $query = $this->db->get($table);
		$row = $query->result_array();
		return $row;
	}

	function get_select_max($table,$select='sort',$where=NULL){
	    $this->db->select_max($select);
	    if (! empty($where)){
	        $this->db->where($where);
	    }
	    $query =$this->db->get($table);
	    if ($query->num_rows() > 0){
	        $row = $query->row_array();
	        $row[$select]++;
	    }else{
	        $row[$select]='1';
	    }
	    return $row;
	}
	
	function get_show($table,$id){
		$this->db->where('id',$id);
		$query =$this->db->get($table);
		$row = $query->row_array();
		return $row;
	}
	
	function get_num($table){
		$this->db->select('count(*) as num');
		$query =$this->db->get($table);
		$row = $query->row_array();
		return $row['num'];
	}
	
	function get_field($table,$select,$where){
	    $this->db->select($select);
	    $this->db->where($where);
	    $query =$this->db->get($table);
	    $row = $query->row_array();
	    return $row[$select];
	}
	
	function set_field($table,$keys,$values,$id) {
	    $data=array();
	    $data[$key] = $value;
		$this->db->where_in('id',$id);
		return $this->db->update($table,$data);
	}
	
	function get_root_id($parent_id,$id=0){
	    if ($parent_id=='0'){
	        $result=$this->get_select_max('category','root_id',"id !=$id");
	        $row['root_id']=$result['root_id'];
	    }else{
	        $this->db->select('root_id');
	        $this->db->where('id',$parent_id);
	        $query =$this->db->get('category');
	        if ($query->num_rows() > 0){
	            $row = $query->row_array();
	        }else{
	            $row['root_id']='1';
	        }
	    }
	    return $row['root_id'];
	}
	
	function get_depth($parent_id){
	    if ($parent_id=='0'){
	        $row['depth']='1';
	    }else{
	        $this->db->select('depth');
	        $this->db->where('id',$parent_id);
	        $query =$this->db->get('category');
	        if ($query->num_rows() > 0){
	            $row = $query->row_array();
	            $row['depth']++;
	        }else{
	            $row['depth']='1';
	        }
	    }
	    return $row['depth'];
	}
	
	function add($table,$data){
		return $this->db->insert($table,$data);
	}
	
	function edit($table,$data,$id){
		$this->db->where('id', $id);
		return $this->db->update($table,$data);
	}
	
	function del($table,$id){
		$this->db->where_in('id',$id);
		return $this->db->delete($table);
	}
	
	//创建数据表
	function create($table,$fields=array()){
	    $this->load->dbforge();
	    $this->dbforge->add_field($fields);//加载字段
	    $field_id = 'id';
	    $this->dbforge->add_key($field_id, TRUE);//添加主键
	    $attributes = array('ENGINE' => 'MyISAM');
	    return $result=$this->dbforge->create_table($table, TRUE, $attributes);//建表
	}
	
	
}
